#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
#include <QSystemTrayIcon>
#include <QSpinBox>
#include <QCheckBox>
#include <QGroupBox>
#include <QTimer>
#include <QMouseEvent>
#include <QProgressBar>
#include <QFrame>
#include <QPushButton>  // 添加QPushButton头文件包含
#include <QDir>  // 添加QDir头文件包含

// 添加 ResourceMonitor 头文件
#include "resource_monitor.h"
#include "cpu_scheduler.h"  // 包含cpu_scheduler.h以使用SchedulerConfig和CpuScheduler

class Settings {
public:
    bool cpuUsageEnabled;
    bool memoryEnabled;
    bool swapEnabled;
    bool cpuFreqEnabled;
    bool cpuMaxTempEnabled;
    bool avgTempEnabled;
    bool hddTempEnabled;
    bool otherTempEnabled;
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void updateSystemInfo();
    void updateFloatingWindow();
    void quitApplication();
    void iconActivated(QSystemTrayIcon::ActivationReason reason);
    
    // 添加缺失的函数声明
    void toggleSchedulerService(QLabel* statusLabel, QPushButton* toggleButton);
    void resetCpuFrequency();
    void applySchedulerConfig(QSpinBox* intervalSpin, QSpinBox* highFreqSpin, QSpinBox* lowFreqSpin,
                              QSpinBox* highTempSpin, QSpinBox* lowTempSpin, QSpinBox* sustainedSpin,
                              QSpinBox* reductionSpin, QSpinBox* increaseSpin, QSpinBox* minFreqLimitSpin,
                              QCheckBox* tempControlCheckbox, QCheckBox* freqControlCheckbox);
    void resetSchedulerConfig(QSpinBox* intervalSpin, QSpinBox* highFreqSpin, QSpinBox* lowFreqSpin,
                              QSpinBox* highTempSpin, QSpinBox* lowTempSpin, QSpinBox* sustainedSpin,
                              QSpinBox* reductionSpin, QSpinBox* increaseSpin, QSpinBox* minFreqLimitSpin,
                              QCheckBox* tempControlCheckbox, QCheckBox* freqControlCheckbox);
    void saveSchedulerConfig(QSpinBox* intervalSpin, QSpinBox* highFreqSpin, QSpinBox* lowFreqSpin,
                             QSpinBox* highTempSpin, QSpinBox* lowTempSpin, QSpinBox* sustainedSpin,
                             QSpinBox* reductionSpin, QSpinBox* increaseSpin, QSpinBox* minFreqLimitSpin,
                             QCheckBox* tempControlCheckbox, QCheckBox* freqControlCheckbox);
    void updateSchedulerStatus(QLabel* statusLabel, QPushButton* toggleButton);

private:
    void setupUI();
    void setupMonitoring();
    void createFloatingWindow();
    void createSystemTrayIcon();
    void updateSystemTrayIcon();
    
    // 添加缺失的函数声明
    QWidget* createMonitorWidget();
    QWidget* createSchedulerWidget();
    QWidget* createLogWidget();
    QWidget* createHelpWidget();
    
    double getCpuUsage();
    double getMemoryUsage();
    double getSwapUsage();  // 新增swap使用率获取函数
    double getTemperature();
    int getCPUMaxTemperature();
    int getAverageTemperature();
    int getHDDTemperature();
    int getOtherHardwareTemperature();
    int getCpuFrequency(); // 添加CPU频率获取函数声明
    double getDiskUsage();
    double getNetworkUploadSpeed();
    double getNetworkDownloadSpeed();
    
    // 添加鼠标事件处理函数声明
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);

protected:
    bool eventFilter(QObject *obj, QEvent *event) override;
    void closeEvent(QCloseEvent *event) override;

private:
    Settings currentSettings;
    // UI组件
    QLabel *cpuLabel;
    QLabel *memoryLabel;
    QLabel *swapLabel;  // 新增swap标签
    QLabel *temperatureLabel;
    QLabel *diskLabel;
    QWidget *floatingWindow;
    QLabel *floatingCpuLabel;
    QLabel *floatingMemoryLabel;
    QLabel *floatingSwapLabel;  // 新增浮动窗口中的swap标签
    QLabel *floatingCpuFreqLabel;
    QLabel *floatingCpuMaxTempLabel;
    QLabel *floatingAvgTempLabel;
    QLabel *floatingHddTempLabel;
    QLabel *floatingOtherTempLabel;
    QSystemTrayIcon *trayIcon;
    QPoint dragPosition;

    // 新增设置项勾选框
    QCheckBox *cpuUsageCheckBox;
    QCheckBox *memoryCheckBox;
    QCheckBox *swapCheckBox;  // 新增swap勾选框
    QCheckBox *cpuFreqCheckBox;
    QCheckBox *cpuMaxTempCheckBox;
    QCheckBox *avgTempCheckBox;
    QCheckBox *hddTempCheckBox;
    QCheckBox *otherTempCheckBox;

    // 添加settingsGroup成员变量
    QGroupBox *settingsGroup;

    QTimer *updateTimer;
    
    // 添加 ResourceMonitor 成员变量
    ResourceMonitor *m_resourceMonitor;
    
    // 添加用于存储上一次网络数据的成员变量
    quint64 lastBytesSent;
    quint64 lastBytesReceived;
    qint64 lastTime;

    // 新增：悬浮窗中的内存使用率标签

    // 新增：主窗口中的CPU频率、CPU最高温度、平均温度、硬盘温度、其他硬件温度标签
    QLabel *cpuFreqLabel;
    QLabel *cpuMaxTempLabel;
    QLabel *avgTempLabel;
    QLabel *hddTempLabel;
    QLabel *otherTempLabel;
    
    // 添加缓存变量用于存储系统信息数据
    double cachedCpuUsage;
    double cachedMemoryUsage;
    double cachedSwapUsage;  // 新增swap使用率缓存
    int cachedCpuFreq;
    int cachedCpuMaxTemp;
    int cachedAvgTemp;
    int cachedHddTemp;
    int cachedOtherTemp;
};

#endif // MAINWINDOW_H